package com.gym.controller;

import com.github.pagehelper.PageInfo;
import com.gym.pojo.Employee;
import com.gym.service.StaffdbService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.MD5Util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StaffdbController {
    @Resource
    private StaffdbService sser;


    @RequestMapping("/login")
    public Employee login(HttpServletRequest request,String account, String password){
        Employee emp=sser.login(account, MD5Util.MD5(account,password));
        System.out.println("蔡鹏狗日的测试中emp:"+emp);
        if(emp!=null){
            HttpSession session = request.getSession();
            session.setAttribute(emp.getEmpname()+emp.getEmpId(),emp);
        }
        return emp;
    }
    @RequestMapping("/selectpwd")
    public Employee selectpwd(String account,String password){
        Employee em = sser.selectpwd(account,MD5Util.MD5(account,password));
        return em;
    }
    @RequestMapping("/selaccount")
    public List<Employee> selaccount(String account){
        return sser.selaccount(account);
    }
    @RequestMapping("/selempid")
    public Employee selempid(int empId){
        return sser.selempid(empId);
    }
    @RequestMapping("/selemp")
    public Map<String,Object> select(String empname,int page,int limit){
        PageInfo<Employee> pageInfo = (PageInfo<Employee>) sser.select(empname,page,limit);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","");
        result.put("count",pageInfo.getTotal());
        result.put("data",pageInfo.getList());
        return result;
    }
    @RequestMapping("/insert")
    public boolean insert(Employee em){
        em.setPassword(MD5Util.MD5(em.getAccount(),em.getPassword()));
        System.out.println("蔡鹏狗日的测试中emp:"+em.getPassword());
        return sser.insert(em);
    }
    @RequestMapping("/update")
    public boolean update(Employee em){
        return sser.update(em);
    }
    @RequestMapping("/updatepwd")
    public boolean updatepwd(Employee em){
        em.setPassword(MD5Util.MD5(em.getAccount(),em.getPassword()));
        return sser.updatepwd(em);}
    @RequestMapping("/delete")
    public boolean delete(int empId){
        return sser.delete(empId);
    }
}
